/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shoppingsystem.view.reports;

import com.shoppingsystem.reports.CreadorReportes;
import com.shoppingsystem.reports.FabricaReportes;
import com.shoppingsystem.reports.Reporte;
import java.awt.BorderLayout;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.sf.jasperreports.swing.JRViewer;

/**
 *
 * @author victor
 */
public class VisorReportes extends javax.swing.JPanel {

    private final FabricaReportes fabricaReportes = new FabricaReportes();

    /**
     * Creates new form VisorReportes
     */
    public VisorReportes() {
        initComponents();       
        llenarLista();
    }
    
    private void llenarLista(){
        for (int i = 0; i < CreadorReportes.REPORTES_DISPONIBLES; i++){
            try {
                listaReportesComboBox.addItem(fabricaReportes.crearReporte(i));
            } catch (Exception ex) {
                Logger.getLogger(VisorReportes.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contenedorPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        listaReportesComboBox = new javax.swing.JComboBox();
        verButton = new javax.swing.JButton();

        contenedorPanel.setBackground(new java.awt.Color(176, 239, 174));
        contenedorPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        contenedorPanel.setName("contenedorPanel"); // NOI18N
        contenedorPanel.setLayout(new java.awt.BorderLayout());

        jLabel1.setBackground(new java.awt.Color(163, 2, 11));
        jLabel1.setFont(new java.awt.Font("Droid Sans", 0, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Módulo de Reportes");
        jLabel1.setName("jLabel1"); // NOI18N
        jLabel1.setOpaque(true);

        jLabel2.setText("Reportes:");
        jLabel2.setName("jLabel2"); // NOI18N

        listaReportesComboBox.setName("listaReportesComboBox"); // NOI18N

        verButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/shoppingsystem/icons/system-search.png"))); // NOI18N
        verButton.setText("Ver");
        verButton.setName("verButton"); // NOI18N
        verButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                verButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 799, Short.MAX_VALUE)
                    .addComponent(contenedorPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(listaReportesComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(verButton, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(listaReportesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(verButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(contenedorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void verButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_verButtonActionPerformed
        try {
            Reporte reporte = fabricaReportes.crearReporte(
                    listaReportesComboBox.getSelectedIndex());
            if (listaReportesComboBox.getSelectedIndex() == 3){
                reporte.añadirParametro("ID_PRODUCTO", 
                        JOptionPane.showInputDialog(this, "Escriba el código del producto:"));
            }
            final JRViewer jv = new JRViewer(reporte.generar());

            contenedorPanel.removeAll();
            contenedorPanel.add(jv, BorderLayout.CENTER);
            contenedorPanel.revalidate();
            
        } catch (Exception ex) {
            Logger.getLogger(VisorReportes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_verButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel contenedorPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JComboBox listaReportesComboBox;
    private javax.swing.JButton verButton;
    // End of variables declaration//GEN-END:variables
}
